﻿using JNPF.Basics.Models;
using JNPF.Basics.Models.VisualDev;
using JNPF.Basics.Models.VisualDevModelData;
using JNPF.Utils;
using JNPF.Utils.Helper;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 0代码功能数据表
    /// 版 本：V2.6.200509
    /// 版 权：引迈信息技术有限公司（https://www.yinmaisoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2020-10-28
    /// </summary>
    public class VisualDevModelDataBll
    {
        private VisualDevModelDataIService service = new VisualDevModelDataService();

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="input">分页查询条件</param>
        /// <returns></returns>
        public async Task<PageResult<Dictionary<string, object>>> GetListResult(VisualDevEntity data, VisualDevModelListQueryInput input)
        {
            try
            {
                return await service.GetListResult(data, input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取单条信息时，主表将时间字符串转时间戳，子表将字符串类型的数组时间戳转成Json数组时间戳
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<VisualDevModelDataInfoOutput> GetTableInfo(string id, VisualDevEntity data)
        {
            try
            {
                return await service.GetTableInfo(id, data);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<VisualDevModelDataEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<VisualDevModelDataInfoOutput> GetTableInfoDataChange(string id, VisualDevEntity visualdevEntity)
        {
            try
            {
                return await service.GetInfo(id, visualdevEntity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<string> GetInfoDataChange(string id, VisualDevEntity visualdevEntity)
        {
            try
            {
                return await service.GetInfoDataChange(id, visualdevEntity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<string> autoFeilds(VisualDevEntity entity, string data)
        {
            try
            {
                return await service.autoFeilds(entity, data);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<int> Create(VisualDevEntity visualdevEntity, VisualDevModelDataCrForm visualdevModelDataCrForm)
        {
            try
            {
                return await service.Create(visualdevEntity, visualdevModelDataCrForm);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<int> Update(string id, VisualDevEntity visualdevEntity, VisualDevModelDataCrForm visualdevModelDataUpForm)
        {
            try
            {
                return await service.Update(id, visualdevEntity, visualdevModelDataUpForm);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<int> TableDelete(string id, VisualDevEntity visualdevEntity)
        {
            try
            {
                return await service.TableDelete(id, visualdevEntity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<int> Delete(VisualDevModelDataEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<PageResult<VisualDevModelDataEntity>> GetList(VisualDevModelListQueryInput input, string modelId)
        {
            try
            {
                return await service.GetList(modelId, input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        public async Task<List<VisualDevModelDataEntity>> GetList(string modelId)
        {
            try
            {
                return await service.GetList(modelId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public List<VisualDevModelDataEntity> deleteKey(List<VisualDevModelDataEntity> list, String[] keys)
        {
            foreach (var entity in list)
            {
                if (!string.IsNullOrEmpty(entity.Data) && keys.Length > 0)
                {
                    Dictionary<string, object> keyMap = entity.Data.ToObject<Dictionary<string, object>>();
                    Dictionary<string, object> keyResult = new Dictionary<string, object>();
                    foreach (var selkey in keys)
                    {
                        foreach (var entry in keyMap)
                        {
                            string key = entry.Key;
                            if (key.Equals(selkey))
                            {
                                keyResult[key] = entry.Value;
                            }
                        }
                    }
                    entity.Data = SerializeHelper.SerializeToString(keyResult);
                }
            }
            return list;
        }

        /// <summary>
        /// 导出数据关键字过滤
        /// </summary>
        /// <param name="list"></param>
        /// <param name="input"></param>
        /// <param name="visualdevEntity"></param>
        /// <returns></returns>
        public async Task<PageResult<Dictionary<string, object>>> exportData(List<VisualDevModelDataEntity> list, VisualDevModelListQueryInput input, VisualDevEntity entity)
        {
            try
            {
                return await service.exportData(list, input, entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public List<FieldsModel> TemplateDataConversion(List<FieldsModel> fieldsModelList)
        {
            try
            {
                return service.TemplateDataConversion(fieldsModelList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        public VisualDevModelDataExportOutput createModelExcel(string formData, string templateFilePath, PageResult<Dictionary<string, object>> realList, string[] keys, UserInfo userInfo)
        {
            List<ExcelTemplateModel> templateList = new List<ExcelTemplateModel>();
            VisualDevModelDataExportOutput output = new VisualDevModelDataExportOutput();
            FormDataModel formDataModel = TemplateKeywordsHelper.ReplaceKeywords(formData).ToObject<FormDataModel>();
            var modelList = service.TemplateDataConversion(formDataModel.fields);
            List<string> columnList = new List<string>();
            try
            {
                ExcelConfig excelconfig = new ExcelConfig();
                excelconfig.FileName = CommonHelper.Guid + ".xls";
                excelconfig.HeadFont = "微软雅黑";
                excelconfig.HeadPoint = 10;
                excelconfig.IsAllSizeColumn = true;
                excelconfig.ColumnModel = new List<ExcelColumnModel>();
                foreach (var item in keys)
                {
                    var excelColumn = modelList.Find(t => t.__vModel__ == item);
                    if (excelColumn != null)
                    {
                        excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = item, ExcelColumn = excelColumn.__config__.label });
                        columnList.Add(excelColumn.__config__.label); ;
                    }
                }
                var addPath = ConfigurationKey.TemporaryFilePath + excelconfig.FileName;
                ExcelExportHelper<Dictionary<string, object>>.Export(realList.list, excelconfig, addPath, columnList);
                output.name = excelconfig.FileName;
                output.url = "/api/Common/Download?encryption=" + userInfo.Id + "|" + excelconfig.FileName;
                return output;
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}
